package com.ylt.unsafe;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

//java.util.ConcurrentModificationException  并发修改异常
public class ListTest {
    public static void main(String[] args) {
        //并发下 ArrayList是不安全的
        /**
         * 解决方案：
         * 1. List<String> list1 = new Vector<>();
         * 2. List<String> list1 = Collections.synchronizedList(new ArrayList<>());
         * 3. List<String> list1 = new CopyOnWriteArrayList<>();
         */

        //CopyOnWrite 写入时复制 COW 计算机程序设计领域的一种优化策略
        List<String> list = Arrays.asList("1","2","3");
        list.forEach(System.out::println);

        List<String> list1 = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                list1.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(list1);
            },String.valueOf(i)).start();

        }
    }
}
